package com.sne.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sne.sys.entity.SysDept;
import com.sne.sys.entity.SysPost;
import com.sne.sys.entity.SysUserDept;
import com.sne.sys.entity.SysUserPost;
import com.sne.sys.mapper.SysPostMapper;
import com.sne.sys.service.ISysPostService;
import com.sne.sys.service.ISysUserPostService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 岗位 服务实现类
 * @author fanghh
 * @date 2020/9/14
 */
@Service
public class SysPostServiceImpl extends ServiceImpl<SysPostMapper, SysPost> implements ISysPostService {

    @Autowired
    private ISysUserPostService iSysUserPostService;

    @Override
    public IPage<SysPost> findPage(Page page, SysPost post) {
        LambdaQueryWrapper<SysPost> queryWrapper = Wrappers.<SysPost>query().lambda();
        if(StringUtils.isNoneBlank(post.getPostCode())){
            queryWrapper.like(SysPost::getPostCode,post.getPostCode());
        }
        if(StringUtils.isNoneBlank(post.getPostName())){
            queryWrapper.like(SysPost::getPostName,post.getPostName());
        }
        if(post.getState() !=null){
            queryWrapper.eq(SysPost::getState,post.getState());
        }
        return this.page(page,queryWrapper.orderByAsc(SysPost::getSort)
                .orderByDesc(SysPost::getCreateTime));
    }

    @Override
    public boolean checkUnique(SysPost post) {
        SysPost sysPost = this.getOne(Wrappers.<SysPost>query().lambda()
                .eq(SysPost::getPostCode, post.getPostCode()));
        return (sysPost == null || post.getId() != null) &&
                (sysPost == null || post.getId().equals(sysPost.getId()));
    }

    @Override
    public boolean delete(Long id) {
        iSysUserPostService.remove(Wrappers.<SysUserPost>query()
                .lambda().eq(SysUserPost::getPostId,id));
        this.removeById(id);
        return true;
    }

    @Override
    public boolean deleteIds(List<Long> ids) {
        iSysUserPostService.remove(Wrappers.<SysUserPost>query()
                .lambda().in(SysUserPost::getPostId,ids));
        this.removeByIds(ids);
        return true;
    }

    @Override
    public List<SysPost> findPostByUser(Long id) {
        List<SysUserPost> posts = iSysUserPostService.list(Wrappers.<SysUserPost>query().lambda().eq(SysUserPost::getUserId, id));
        List<Long> ids = posts.stream().map(SysUserPost::getPostId).collect(Collectors.toList());
        if(CollectionUtils.isNotEmpty(ids)){
            return this.list(Wrappers.<SysPost>query().lambda().in(SysPost::getId, ids));
        }
        return null;
    }
}
